﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FF.Entity;
using FF.Method;

namespace FF
{
    public partial class FormUserModify : Form
    {
        public User currentUser;
        public User user;
        public FormUserModify()
        {
            InitializeComponent();
        }
        public FormUserModify(User cuser)
        {
            currentUser = cuser;
            InitializeComponent();
        }

        private void FormModifyUser_Load(object sender, EventArgs e)
        {
            textBoxUsername.Text = user.UserName;
            textBoxUserId.Text = user.UserId.ToString();
            textBoxPassword.Text = user.Password;
            richTextBoxRemark.Text = user.Remark;
            //string sql = "select type,name from ff_user_type where type = '" + user.Type + "'";
            string sql = "select type,name from ff_user_type ";
            DataSet ds = SQLiteHelper.Query(sql);
            comboBox1.DataSource = ds.Tables[0];
            comboBox1.DisplayMember = "name";
            comboBox1.ValueMember = "type";
            comboBox1.SelectedValue = user.Type;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定删除当前用户吗？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if(user.UserId == currentUser.UserId)
                {
                    MessageBox.Show("不能删除自己");
                    return;
                }
                if(UserMethod.userDelete(user.UserId.ToString()) == 1)
                {
                    MessageBox.Show("删除成功");
                    this.Close();
                }else
                {
                    MessageBox.Show("删除失败");
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if(textBoxUsername.Text == "" || textBoxPassword.Text == "")
            {
                MessageBox.Show("姓名或密码不能为空！", "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            user.UserName = textBoxUsername.Text.Trim();
            user.Password = textBoxPassword.Text.Trim();
            user.Remark = richTextBoxRemark.Text;
            user.Type = comboBox1.SelectedValue.ToString();
            if(UserMethod.userUpdate(user) == 1)
            {
                 MessageBox.Show("修改成功");
                this.Close();
            }else
            {
                MessageBox.Show("修改失败");
            }
            
        }
    }
}
